<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'user_add.jsp' starting page</title>
    
    <link rel="stylesheet" type="text/css" href="js/themes/default/easyui.css">
	<link rel="stylesheet" type="text/css" href="js/themes/icon.css">
    <script type="text/javascript" src="js/jquery.min.js"></script>
	<script type="text/javascript" src="js/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="js/locale/easyui-lang-zh_CN.js"></script>
    
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<script type="text/javascript">
		$(function(){
			  $.extend($.fn.validatebox.defaults.rules, {   	
				      minLength: {   	
				          validator: function(value, param){   	
				              return value.length >= param[0];   	
				          },   	
				          message: ''  	
				      }  ,					   
				  }); 			   
			  
			  $('#btn').click(function(){
					$('#myform').form('submit',{
							url:'json/item_add.action' , 
							onSubmit:function(){
								if(!$('#myform').form('validate')){
									return false ;		//当表单验证不通过的时候 必须要return false 
								}
							} ,
							success:function(){																	
								location.reload(true);
								alert("成功");
							}
					});
				});
			  
			  $('#code').blur(function(){
				  var value = $('#code').val();
				  document.getElementById("errorMsg").innerHTML = "正在验证.....";
				  $.ajax({
						url:'json/item_isExist.action',
						cache:false,
						data:{code:value},
						dataType:'json',
						success:function(result){
							if(result.status == "fail"){
								document.getElementById("errorMsg").innerHTML = result.msg;	//该物料是否存在，会在相应的div显示
								myform.code.focus();
							} else {
								document.getElementById("errorMsg").innerHTML = result.msg;
							}
						}	
					});	
			  });
			  $('#price').numberbox({   			      
				  min:0,   				     
				  precision:0,
				  required:true,
				  missingMessage:'单价必填'
			  });
			  <%--
			  不知道出于何种原因，添加这个的话系统会找不到action and result，即使你配对了action
			  原因：这个数值框允许两个小数位，而实体类只是一个整数，解决方法可以改实体类和数据库，也可以改这个数值框
			  $('#price').numberbox({   			      
				  min:0,   				     
				  precision:2,
				  required:true,
				  missingMessage:'单价必填'
			  });  
			--%>
		});
		
	</script>

  </head>
  
  <body>
	<h2>添加物料</h2>  
	<hr>
	<center>
	  	<form id="myform" action="json/item_add.action" method="post">
	  		<table>
	  			<tr>
	  				<td>物料代码</td>
	  				<td>
	  					<input type="text" id="code" name="item.code" class="easyui-validatebox" required=true 
	  						missingMessage="物料代码必填" validType="minLength[5]" invalidMessage="物料代码至少5个字符"
	  						style="width:200px;">
	  					<font color="red"><span id="errorMsg"></span></font> 	  					
	  				</td>	  				 				
	  			</tr>
	  			<tr>
	  				<td>物料名称</td>
	  				<td>
	  					<input type="text" name="item.name" class="easyui-validatebox" required=true 
	  						missingMessage="物料名必填" validType="minLength[3]" invalidMessage="物料名至少3个字符"
	  						style="width:200px;">	  					
	  				</td>
	  			</tr>
	  			<tr>
	  				<td>物料规格</td>
	  				<td>
	  					<input type="text" name="item.spec" class="easyui-validatebox" required=true 
	  						missingMessage="物料规格不能为空" style="width:200px;">
	  				</td>
	  			</tr>
	  			<tr>
	  				<td>物料型号</td>
	  				<td>
	  					<input type="text" name="item.pattern" class="easyui-validatebox" required=true 
	  						missingMessage="物料型号不能为空" style="width:200px;">
	  				</td>
	  			</tr>
	  			<tr>
	  				<td>计量单位</td>
	  				<td>
	  					<input type="text" name="item.unit" class="easyui-validatebox" required=true 
	  						missingMessage="计量单位不能为空" style="width:200px;">
	  				</td>
	  			</tr>
	  			<tr>
	  				<td>物料类别</td>
	  				<td>
	  					 <select name="item.category" style="width:200px;" class="easyui-validatebox" 
	  					 		required=true missingMessage="物料类别不能为空">  
						      <option value="药剂">药剂</option>  					
						      <option value="药丸" selected="selected">药丸</option>  					
						      <option value="药膏">药膏</option>  											
						  </select> 
	  				</td>
	  			</tr>
	  			<tr>
	  				<td>单价</td>
	  				<td>
	  					<input id="price" type="text" name="item.price" style="width:200px;">
	  				</td>
	  			</tr>
	  			<tr>
	  				<td></td>
	  				<td>
	  					<a id="btn" class="easyui-linkbutton">添加</a>
	  				</td>
	  			</tr>
	  		</table>	
	  	</form>	
  	</center>
  	
  </body>
</html>
